package com.bsj.pay.util.platform.wsgw.sm;

public class SM4Utils
{
    public byte[] decryptData_CBC(byte[] sm4_crypt_cbc, final byte[] array, final byte[] array2) {
        try {
            final SM4_Context sm4_Context = new SM4_Context();
            sm4_Context.isPadding = true;
            sm4_Context.mode = 0;
            final SM4 sm4 = new SM4();
            sm4.sm4_setkey_dec(sm4_Context, sm4_crypt_cbc);
            sm4_crypt_cbc = sm4.sm4_crypt_cbc(sm4_Context, array, array2);
            return sm4_crypt_cbc;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            sm4_crypt_cbc = null;
            return sm4_crypt_cbc;
        }
    }
    
    public byte[] decryptData_ECB(byte[] array, byte[] sm4_crypt_ecb) {
        try {
            final SM4_Context sm4_Context = new SM4_Context();
            sm4_Context.isPadding = true;
            sm4_Context.mode = 0;
            final SM4 sm4 = new SM4();
            sm4.sm4_setkey_dec(sm4_Context, array);
            sm4_crypt_ecb = sm4.sm4_crypt_ecb(sm4_Context, sm4_crypt_ecb);
            int length = sm4_crypt_ecb.length;
            for (int n = sm4_crypt_ecb.length - 1; n >= 0 && sm4_crypt_ecb[n] == 0; --n, --length) {}
            array = new byte[length];
            System.arraycopy(sm4_crypt_ecb, 0, array, 0, length);
            return array;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            array = null;
            return array;
        }
    }
    
    public byte[] encryptData_CBC(byte[] sm4_crypt_cbc, final byte[] array, final byte[] array2) {
        try {
            final SM4_Context sm4_Context = new SM4_Context();
            sm4_Context.isPadding = true;
            sm4_Context.mode = 1;
            final SM4 sm4 = new SM4();
            sm4.sm4_setkey_enc(sm4_Context, sm4_crypt_cbc);
            sm4_crypt_cbc = sm4.sm4_crypt_cbc(sm4_Context, array, array2);
            return sm4_crypt_cbc;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            sm4_crypt_cbc = null;
            return sm4_crypt_cbc;
        }
    }
    
    public byte[] encryptData_ECB(byte[] sm4_crypt_ecb, final byte[] array) {
        try {
            final SM4_Context sm4_Context = new SM4_Context();
            sm4_Context.isPadding = true;
            sm4_Context.mode = 1;
            final SM4 sm4 = new SM4();
            sm4.sm4_setkey_enc(sm4_Context, sm4_crypt_ecb);
            sm4_crypt_ecb = sm4.sm4_crypt_ecb(sm4_Context, array);
            return sm4_crypt_ecb;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            sm4_crypt_ecb = null;
            return sm4_crypt_ecb;
        }
    }
}
